package com.egoo.ticket.server.utils.common;

import com.egoo.ticket.server.config.redis.RedisKeyConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author：victor_tang
 * @Date ：2019/10/6
 * @Description：编码生成工具类
 */
@Service
@Component
@Slf4j
public class CreateCodeUtil {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private RedisKeyConstant redisKeyConstant;

	public static SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd");


	/**
	 * TODO 生成随机id
	 *
	 * @param
	 * @author: victor_tang
	 * @createtime: 2020/1/2 14:39
	 * @return: java.lang.String
	 */
	public String getUuid() {
		UUID randomUuid = UUID.randomUUID();
		return randomUuid.toString().replace("-", "");
	}

	/**
	 * TODO 生成工单日志id
	 *
	 * @param
	 * @author: victor_tang
	 * @createtime: 2020/1/2 14:33
	 * @return: java.lang.String
	 */
	public String getWorkOrderLogId() {
		return getUuid();
	}

	/**
	 * TODO 生成触发器id
	 *
	 * @param
	 * @author: victor_tang
	 * @createtime: 2020/1/2 14:33
	 * @return: java.lang.String
	 */
	public String getTriggerId() {
		return getUuid();
	}

	/**
	 * @return java.lang.String
	 * @Author Donny_Dong
	 * @Description //TODO 生成工单id
	 * @Date 15:55 2020/1/14
	 * @Param []
	 **/
	public String getWorkOrderId() {
		synchronized (sdfDate) {
			String currentDate = sdfDate.format(new Date());
			Object numObj = redisUtil.get(RedisKeyConstant.WORK_ORDER_ID_NUM);
			if (numObj == null) {
				// 为空时，代表当天第一个工单
				redisUtil.set(RedisKeyConstant.WORK_ORDER_ID_NUM, "1");
				return currentDate + "0001";
			} else {
				// 不为空，累计增加，并更新redis
				String newNumStr = Integer.valueOf(numObj.toString()) + 1 + "";
				redisUtil.set(RedisKeyConstant.WORK_ORDER_ID_NUM, newNumStr);
				return currentDate + transformNum(newNumStr);
			}
		}
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 获取工单关联关系表id
	 * @Param:
	 * @return:
	 * @Date: 2020/4/24
	 */
	public String getWorkOrderRelationId(){
		return getUuid();
	}

	/**
	 * TODO 转化工单id编号显示方式
	 *
	 * @param numStr 1
	 * @author: victor_tang
	 * @createtime: 2020/3/6 11:19
	 * @return: java.lang.String
	 */
	private String transformNum(String numStr) {
		Integer num = Integer.valueOf(numStr);
		if (numStr.length() == 1) {
			return "000" + num;
		} else if (numStr.length() == 2) {
			return "00" + num;
		} else if (numStr.length() == 3) {
			return "0" + num;
		} else if (numStr.length() == 4) {
			return num.toString();
		}
		return null;
	}

	/**
	 * TODO 生成任务签到id
	 *
	 * @param
	 * @author: victor_tang
	 * @createtime: 2020/1/2 14:33
	 * @return: java.lang.String
	 */
	public String getTaskSignInId() {
		return getUuid();
	}

	/**
	 * 获取头部信息表id
	 * @return
	 */
	public String getTableHead() {

		return getUuid();
	}
}
